<template>
	<cl-crud ref="crud" @load="onLoad" :on-refresh="onRefresh">
		<cl-data-table
			ref="table"
			id="shopDayInfo"
			v-bind="table"
			:data="list"
			:loading="loading"
			auto-page
			full
		>
			<!-- <cl-pagefull @resize="onResize"> -->
			<template #head>
				<el-row type="flex">
					<cl-filter-group v-model="search" @reset="changeBiz">
						<cl-filter label="所属事业部">
							<cl-select
								v-model="search.biz_id"
								placeholder="请选择"
								filterable
								@change="changeBiz"
								style="width: 150px"
							>
								<el-option
									v-for="item in business"
									:key="item.biz_id"
									:label="item.biz_name"
									:value="item.biz_id"
								/>
							</cl-select>
						</cl-filter>
						<cl-filter label="时间">
							<cl-date-picker
								v-model="search.date"
								type="daterange"
								range-separator="至"
								start-placeholder="开始日期"
								end-placeholder="结束日期"
								value-format="yyyy-MM-dd"
								style="width: 210px"
								:clearable="false"
							/>
						</cl-filter>
					</cl-filter-group>
					<cl-flex1 />
					<cl-table-handle
						:print="printParams"
						:columns="initnewColumns"
						v-model="table.columns"
						@confirm="filterConfirm"
						@export-file="exportFile"
					/>
				</el-row>
			</template>

			<template #column-cus_type_text="{ scope: { row } }">
				<div
					class="cus_type"
					:class="{
						'color-primary': row.cus_type == 1,
						old: row.cus_type == 2,
						'color-danger': row.cus_type == 3,
						'color-warning': row.cus_type == 4
					}"
				>
					{{ row.cus_type_text }}
				</div>
			</template>

			<template #column-opt="{ scope: { row } }">
				<div class="flex flex-align-start">
					<el-button
						type="primary"
						v-auth="'store_cusdetailindex_look'"
						@click.stop="openDetail(row)"
					>
						详情
					</el-button>
				</div>
			</template>
			<cl-empty slot="empty" />
		</cl-data-table>
	</cl-crud>
</template>

<script>
import dayjs from "dayjs";
import constant from "@/constant";
import { exportExcelCoustom } from "@/utils/excel";
import { constFilter } from "@/filters";
import teacherSelect from "@/components/teacher-select";

export default {
	name: "FinanceShopDayInfo",
	inject: ["$layout"],
	components: {
		teacherSelect
	},
	data() {
		return {
			printParams: {
				id: "refundReport",
				popTitle: "退款报表"
			},
			// 表格配置
			table: {
				"context-menu": ["refresh"],
				columns: [],
				showSummary: true,
				// summaryMethod: _.throttle(this.getSummaries, 50, { 'leading': true, 'trailing': true }),
				summaryMethod: () => {
					return this.summariesArr;
				},
				// summaryMethod: this.getSummaries,
				showPage: true,
				limit: 20
			},
			initnewColumns: [],
			initColumns: [
				{ label: "退款日期", prop: "created_date", width: 120, fixed: true, disable: true },
				{ label: "订单号", prop: "order_no", width: 190, fixed: true, disable: true },
				{
					label: "客户姓名",
					prop: "cus_name",
					minWidth: 90,
					fixed: true,
					align: "left",
					showOverflowTooltip: true
				},
				{ label: "会员号", prop: "cus_id", align: "left", width: 90, fixed: true },
				{
					label: "大区",
					prop: "first_zone",
					minWidth: 100,
					fixed: true,
					align: "left",
					showOverflowTooltip: true
				},
				{
					label: "门店",
					prop: "dept_name",
					minWidth: 220,
					fixed: true,
					align: "left",
					showOverflowTooltip: true
				},
                { label: "订单日期", prop: "appoint_date", width: 120},
                { label: "退款金额", prop: "refund_money", width: 90},
                { label: "退款类型", prop: "pay_name", width: 90},
				{ label: "操作", prop: "opt", width: 100, disable: true, fixed: "right" }
			],
			search: {
				biz_id: "", //事业部
				date: [
					dayjs()
						.startOf("day")
						.format("YYYY-MM-DD"),
					dayjs()
						.endOf("day")
						.format("YYYY-MM-DD")
				]
			},
			loading: false,
			list: [],
			summariesArr: [],
			// 老师
			sourceList: []
		};
	},
	computed: {
		bizRefData() {
			let { biz_id } = this.search;
			return this.getBizRefData(biz_id);
		}
	},
	created() {
		this.search.biz_id = this.getCurrBiz();
		this.init();
	},
	methods: {
		async init() {
			const { biz_id } = this.search;
		},
		onLoad({ ctx, app }) {
			ctx.service(this.$service.finance)
				.set("dict", {
					api: {
						page: "refundReport"
					}
				})
				.done();
			// app.refresh(this.search);
		},
		async onRefresh(params, { next, render }) {
			this.loading = true;
			let initColumnsArr = [];
			initColumnsArr = [...this.initColumns];
			let dataList = await next(params).finally(() => {
				this.loading = false;
			});
            console.log(dataList)
			// const dataList = data_list.map(item => {
			// 	return {
			// 		...item,
			// 		cus_type_text: item.cus_type ? constFilter(item.cus_type, "CUS_TYPE") : "",
			// 		items_prod_type: item.items_prod_type
			// 			? constFilter(item.items_prod_type, "ITEMS_PROD_TYPE")
			// 			: "",
			// 		is_rest: item.is_rest ? "是" : "否"
			// 	};
			// });
			this.list = dataList;

			// if (title_list.length) {
			// 	let titleList = title_list.map((v, i) => ({
			// 		label: v.name,
			// 		prop: v.key,
			// 		width: 100
			// 	}));
			// 	initColumnsArr.splice(initColumnsArr.length - 1, 0, ...titleList);
			// }
			this.getSummaries({ columns: initColumnsArr });

			this.initnewColumns = initColumnsArr;
			this.table.columns = initColumnsArr;

			render(dataList);

			// this.$nextTick(() => {
			// 	this.$refs.table.doLayout();
			// })
		},
		// crud刷新
		refresh() {
			this.$refs.crud.refresh({ page: 1, ...this.search });
		},
		onResize(h) {
			this.table.props.maxHeight = h;
		},
		// 详情
		openDetail(row) {
			this.$layout.openCusDetail(row, "order");
		},
		// 表格合计
		getSummaries({ columns, data }) {
			let ignore = [
				"appoint_date",
				"cus_name",
				"cus_id",
				"biz_name",
				"company_name",
				"items_sell_teacher",
				"items_adviser_teacher",
				"bra_name",
				"cus_type",
				"items_f_category_name",
				"items_prod_name",
				"is_rest",
				"items_prod_type",
				"items_teacher_name",
				"store_name",
				"opt",
				"cus_type_text",
				"items_teacher_post",
				"order_no"
			];
			const newArr = columns.map((column, index) => {
				if (index === 0) return "合计";
				if (ignore.includes(column.prop)) return "";
				const values = this.list.map(item => Number(item[column.prop]));
				let sum = values.reduce((prev, curr) => {
					const value = Number(curr);
					if (!isNaN(value)) {
						return prev + curr;
					}
					return prev;
				}, 0);
				return this.toFixed(sum);
			});
			// console.info("this.summariesArr", this.summariesArr)

			this.summariesArr = newArr;
			return newArr;
		},
		async changeBiz(v) {
			this.search = {
				...this.search,
				company_id: "",
				bra_id: [],
				dept_id: "",
				channel_id: ""
			};
			this.init();
		},
		// 导出EXCEl
		exportFile() {
			exportExcelCoustom("退款报表", this.list, this.table.columns, this.summariesArr);
		},
		// 表头筛选
		filterConfirm(columns) {
			this.getSummaries({ columns: columns });
		}
	}
};
</script>
<style lang="scss" scoped>
::v-deep {
	.el-table--scrollable-x {
		.el-table__body-wrapper {
			z-index: 2;
		}
	}
}
</style>
